﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.IO;

namespace SpeedTest
{
	public partial class PanelExecuteSuitePlan : UserControl
	{
		private Data data = null;
		private string itemID = String.Empty;
        private NodeType nodeType = NodeType.Undefined;
		private DataTable planSuiteTable = null;

        public event NavigateEvent Navigate;

		public PanelExecuteSuitePlan(Data data, string itemID, NodeType nodeType)
		{
			InitializeComponent();
			this.data = data;
			this.itemID = itemID;
            this.nodeType = nodeType;

			if (data != null)
			{
				if (nodeType == NodeType.PlanNode)
				{
					planSuiteTable = data.GetPlanByID(itemID);
				}
				else
				{
					planSuiteTable = data.GetSuiteByID(itemID);
				}

				if (planSuiteTable != null)
				{
					browserSteps.DocumentText = LoadSuitePlan();
				}
			}
		}

		private string LoadSuitePlan()
		{
			string title = planSuiteTable.Rows[0]["Description"].ToString();
			string steps = String.Empty;
			if (nodeType == NodeType.SuiteNode) {steps = planSuiteTable.Rows[0]["Steps"].ToString(); }

			return GenerateHtml(steps, title);
		}

		//set the bg color to match the form, regardless of themes
		private string SetDocumentBGColor()
		{
			string r = this.BackColor.R.ToString();
			string g = this.BackColor.G.ToString();
			string b = this.BackColor.B.ToString();
			string style = "background-color: rgb(" + r + ", " + g + ", " + b + ");";

			return style;
		}

		private string GenerateHtml(string steps, string header)
		{
			XmlDocument doc = new XmlDocument();
			XmlElement htmlElem = doc.CreateElement("HTML");
			XmlElement body = doc.CreateElement("BODY");
			XmlElement div = doc.CreateElement("DIV");
			div.SetAttribute("ALIGN", "Center");
			body.SetAttribute("STYLE", SetDocumentBGColor());
			doc.AppendChild(htmlElem);
			htmlElem.AppendChild(div);
			div.AppendChild(body);

			XmlElement titleHeader = doc.CreateElement("H3");
			titleHeader.SetAttribute("STYLE", "text-align: center;");
			titleHeader.InnerText = header;
			body.AppendChild(titleHeader);

			body.AppendChild(doc.CreateElement("BR"));

			XmlElement textSpan = doc.CreateElement("SPAN");
			textSpan.InnerXml = GenerateHtmlFromString(doc, steps);
			

			body.AppendChild(textSpan);

			return doc.InnerXml;
		}

		private string GenerateHtmlFromString(XmlDocument doc, string steps)
		{
			XmlElement stringHtml = doc.CreateElement("SPAN");
			StringReader reader = new StringReader(steps);

			while (reader.Peek() > -1)
			{
				XmlElement paragraph = doc.CreateElement("P");
				paragraph.InnerText = reader.ReadLine();
				paragraph.SetAttribute("ALIGN", "Left");

				stringHtml.AppendChild(paragraph);
			}

			return stringHtml.InnerXml;
		}

		private void btnNext_Click(object sender, EventArgs e)
		{
			Navigate.Invoke(NavigationDirection.Next);
		}

		private void btnPrevious_Click(object sender, EventArgs e)
		{
			Navigate.Invoke(NavigationDirection.Previous);
		}
	}
}
